package advanced进阶;

/**
 * 死锁：多个线程同时被阻塞
 * 1.互斥使用
 * 2.不可抢占
 * 3.请求和保持
 * 4.循环等待，最容易解决，对资源进行编号，按顺序去获取数据
 *
 */
public class 死锁 {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread t1 = new Thread(()->{
            synchronized (o1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o2){
                    System.out.println("t1执行");
                }
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (o2){
                synchronized (o1){
                    System.out.println("t2执行");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
